package com.oos.zone.wechat.mp.service.handler;

import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Executor;

/**
 * 功能描述：
 *
 * @author 拓金辉
 * @version 1.0
 * @date 2021-08-08 10:00:24
 */
public abstract class AbstractWxMpMessageHandler implements WxMpMessageHandler {

    @Resource
    public Executor executor;
    public WxMpXmlMessage wxMessage;
    public Map<String, Object> context;
    public WxMpService wxMpService;
    public WxSessionManager sessionManager;
    public final WxMpXmlOutMessage defaultTextMessage = WxMpXmlOutMessage.TEXT().build();

    /**
     * 处理微信推送消息.
     *
     * @param wxMessage      微信推送消息
     * @param context        上下文，如果handler或interceptor之间有信息要传递，可以用这个
     * @param wxMpService    服务类
     * @param sessionManager session管理器
     * @return xml格式的消息，如果在异步规则里处理的话，可以返回null
     */
    @Override
    public final WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) {
        this.wxMessage = wxMessage;
        this.context = context;
        this.wxMpService = wxMpService;
        this.sessionManager = sessionManager;
        return Optional.ofNullable(handle0()).orElse(defaultTextMessage);
    }

    /**
     * 方法描述：真正处理业务的方法
     *
     * @return me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage
     * @author 拓金辉
     * @date 2021-08-08 10:24:41
     */
    public abstract WxMpXmlOutMessage handle0();

    /**
     * 方法描述：获取应用id默认实现
     *
     * @return java.lang.String
     * @author 拓金辉
     * @date 2021-07-31 11:42:35
     */
    public final String appId() {
        return (String) context.getOrDefault("appId", null);
    }

}
